﻿namespace Chameleon.Samples
{
	using Chameleon.Data;
	using System;
	using System.ComponentModel.DataAnnotations;
	using System.Collections.Generic;
	using System.Linq;
	using ELinq = System.Data.Entity;
	using DLinq = System.Data.Linq.Mapping;

	/// <summary>
	/// A basic entity for demonstrating code-first paradigms.
	/// </summary>
	/// <remarks>
	/// This class contains mappings so that it can be used with either Entity Framework or 
	/// Linq to SQL by way of code-first. These mappings are made by way of attributes on
	/// the class itself (class-per-table) and on the properties (property-per-field).
	/// </remarks>
	
	[DLinq.Table(Name="dbo.BasicEntity")]
	public sealed class BasicEntity
	{
		#region Id

			/// <summary>
			/// Gets the unique identifier of the current instance.
			/// </summary>
		
			// entity framework mapping via data annotations
			[Key,Column("Id",TypeName="uniqueidentifier")]

			// linq to sql mapping
			[DLinq.Column(Name="Id",IsPrimaryKey=true,DbType="uniqueidentifier",UpdateCheck=DLinq.UpdateCheck.Never)]
		
			public Guid Id { get; private set; }

		#endregion


		#region Modified

			/// <summary>
			/// Gets or sets when the object was last modified.
			/// </summary>
			
			// entity framework mapping via data annotations
			[Column("Modified",TypeName="datetime")]

			// linq to sql mapping
			[DLinq.Column(Name="Modified",DbType="datetime",UpdateCheck=DLinq.UpdateCheck.Never)]
			public DateTime Modified { get; set; }

		#endregion


		#region Methods

			/// <summary>
			/// Returns the collection of all entities of this type in the depository.
			/// </summary>
			/// <param name="depository">Data context from which to populate entities.</param>
			
			public static IEnumerable<BasicEntity> All( IDepository depository )
			{
				return depository.Query<BasicEntity>().ToArray();
			}


			/// <summary>
			/// Removes the specified items from the depository.
			/// </summary>
			/// <param name="depository">Depository from which to delete the items.</param>
			/// <param name="entities">Items to be removed from the depository.</param>

			public static void Delete( IDepository depository, IEnumerable<BasicEntity> entities )
			{
				foreach ( var e in entities )
				{
					depository.Attach( e );
					depository.Delete( e );
				}

				depository.Submit();
			}


			/// <summary>
			/// Inserts a new item into the depository.
			/// </summary>
			/// <param name="depository">Depository into which to insert the item.</param>

			public static void Insert( IDepository depository )
			{
				depository.Insert( new BasicEntity() { Modified = DateTime.Now } );
				depository.Submit();
			}


			/// <summary>
			/// Updates the specified items within the depository.
			/// </summary>
			/// <param name="depository">Depository in which to perform the update.</param>
			/// <param name="entities">Items to be updated in the depository.</param>

			public static void Update( IDepository depository, IEnumerable<BasicEntity> entities )
			{
				foreach ( var e in entities )
				{
					depository.Attach( e );
					e.Modified = DateTime.Now;
				}

				depository.Submit();
			}

		#endregion


		#region Constructors

			/// <summary>
			/// Initializes a new instance using default values.
			/// </summary>
			
			public BasicEntity()
			{
				Id = Guid.NewGuid();
			}

		#endregion
	}
}
